# ~*~ coding: utf-8 ~*~
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import InputRequired

from apps.common.form import ErrorFormMinx
from apps.common.validators import Number, IPAddress

__all__ = ['AssetCreateUpdateForm']


class AssetCreateUpdateForm(ErrorFormMinx, FlaskForm):
    hostname = StringField(validators=[InputRequired('主机名称不能为空')])
    platform = StringField(validators=[InputRequired('系统平台不能为空')])
    protocol = StringField(validators=[InputRequired('连接协议不能为空')])
    port = StringField(validators=[InputRequired('连接端口不能为空'), Number('端口必须为数字')])
    manage_ip = StringField(validators=[InputRequired('管理IP不能为空'), IPAddress('IP地址格式不正确')])
    public_ip = StringField(validators=[IPAddress('IP地址格式不正确')])
    admin_user_id = StringField(validators=[InputRequired('管理用户不能为空')])
    nodes = StringField()
    labels = StringField()

    class Meta:
        csrf = True

    @property
    def data(self):
        return {
            'hostname': self.hostname.data,
            'platform': self.platform.data,
            'protocol': self.protocol.data,
            'port': int(self.port.data),
            'manage_ip': self.manage_ip.data,
            'public_ip': self.public_ip.data,
            'admin_user_id': self.admin_user_id.data,
            'nodes': self.nodes.data,
            'labels': self.labels.data,
        }
